<?php
/* QQ:34734852
 * 业务联系：小鱼
 */
class files {
	public $error = '';
	
	public function get( $url = '', $IsTidy =  false ) {
		try{
			if ( file_exists( $url ) || preg_match('/^http[s]?\:\/\//is', $url ) ) {
				if ( $IsTidy && class_exists('Tidy') ) {
					$Tidy = new Tidy;
					$Tidy -> parseString(file_get_contents( $url ), array('indent' => true, 'output-xhtml' => false, 'show-body-only' => false, 'wrap' => 200 ), 'UTF8');
					$Tidy -> cleanRepair();
					return $Tidy -> value;
				} else {
					return file_get_contents( $url );
				}
			} else {
				return false;
			}
			return file_exists( $url ) ? file_get_contents( $url ) : false;
		}catch(Exception $e){
			$this -> error = $e -> getMessage();
			return false;
		}
	}
	
	public function put( $file = '', $Content = '', $FILE_APPEND = false ) {
		try{
			if ( $FILE_APPEND ) {
				return file_put_contents( $file, $Content . "\r\n", FILE_APPEND );
			} else {
				return file_put_contents( $file, $Content );
			}
		}catch(Exception $e){
			$this -> error = $e -> getMessage();
			return false;
		}
	}
	
	public function DownLoad( $FileName = '' ) {
		if ( empty( $FileName ) ) { 
		    EyouPHP::ERROR('无下载文件');
		} elseif ( !file_exists( $FileName ) ) {
			EyouPHP::ERROR('下载文件缺失');
		} elseif (!is_file( $FileName )) {
			EyouPHP::ERROR('非文件下载');
		} else {
			$Name = basename( $FileName );
			$Ming = dirname( $FileName ) . "/$Name";
			$Size = sprintf('%d', filesize( $Ming ));
			$Mesg = array(
			  'Content-Type:' . array('application/octet-stream'),
			  "Content-Disposition: attachment; filename={$Name}",
			  'Content-Transfer-Encoding: binary',
			  "Content-Length:{$Size}",
			  'Expires: 0'
			);
			if (isset($_SERVER['HTTP_USER_AGENT']) && ((strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)))  {
				$Mesg[5] = 'Cache-Control: must-revalidate, post-check=0, pre-check=0';
				$Mesg[6] = 'Pragma: public';
			} else {
				$Mesg[5] = 'Pragma: no-cache';
			}
			$CoonTent = $this -> get( $Ming );
			foreach( $Mesg as $heads ) {
				header( $heads );
			}
			echo $CoonTent;
			unset( $CoonTent, $FileName, $heads, $Mesg, $Ming, $Name, $Size );
		}
		
	}
	
	public function xmlread( $File = '', $IsFile =  true, $type = 'ARRAY', $IntNum = false ) {
		$IsFile && $File = $this -> get( $File , false ); 
		if ( !$File ) {
			return false;
		}
		switch( trim(strtolower($type)) ) {
			case   'xml': return $File; break;
			case 'array':
			      $Xml = xml_parser_create('UTF-8');
				  xml_parse_into_struct( $Xml, $File, $Value );
				  xml_parser_free( $Xml );
				  $Xml = null;
				  $Arr = $Xia = array();
				  foreach( $Value as $array ) {
					  foreach( $array as $key => $smallarray ) {
						  if ( $key == 'attributes' ) {
							  if ( $IntNum ) {
								  foreach( $smallarray as $k => $v ) {
									  $Xia[strtolower($k)] = $v;
								  }
								  $Arr[] = $Xia;
							  } else {
								  foreach( $smallarray as $k => $v ) {
									$Arr[ strtolower($k) ] = $v;
								  }
							  }
						  }
					  }
				  }
				  unset( $Xml, $Value, $array, $key, $smallarray, $k, $v, $Xia, $IntNum );
				  return $Arr;
				  break;
		   default: return false;
		}
	}
		
	
}